package com.wulis.config.security;

import static com.wulis.common.model.enums.HttpStatusEnum.UNAUTHORIZED;
import static com.wulis.config.security.AccessDeniedHandlerImpl.jsonResponse;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import com.wulis.common.wrapper.ApiWrapMapper;
import com.wulis.config.swagger.SwaggerSecurityConfig;

/**
 * @author WuliBao
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Resource
    private SwaggerSecurityConfig swaggerSecurityConfig;
    
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
        AuthenticationException e)
        throws IOException {
        // swagger doc.html basic认证
        swaggerSecurityConfig.basicAuthentication(httpServletRequest, httpServletResponse, e);
        jsonResponse(httpServletResponse, ApiWrapMapper.wrap(UNAUTHORIZED));
    }
}
